package com.ruanko.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.DBUtil.SQLHelper;
import com.ruanko.dao.DownloadFileDao;
import com.ruanko.entity.DownloadFileBean;
import com.ruanko.entity.UploadFileBean;

public class DownloadFileDaoImpl implements DownloadFileDao{
	private SQLHelper sqlHelper = new SQLHelper();
	private ArrayList<DownloadFileBean> downLoadFileList = new ArrayList<DownloadFileBean>();
	/**
	 * 插入下载的文件记录
	 * @param uploadFileList
	 * @return
	 */
	public boolean insert(List<DownloadFileBean> downLoadFileList){
		boolean b = false;
		try {
			System.err.println("************8\n"+downLoadFileList.size()+"\n***********888");
			String sql = "insert into download_file (downloadTime,aFilename,userid) values(?,?,?)";
			String[] params = new String[downLoadFileList.size()*3];
			int parmLenght = 0;
			if(!downLoadFileList.isEmpty()){
				for (int i = 0; i < downLoadFileList.size(); i++) {
					
						if (i != 0) {
							sql = sql + ",(?,?,?)";
						}
						
						params[parmLenght++] = downLoadFileList.get(i).getDownloadTime();
						params[parmLenght++] = downLoadFileList.get(i).getaFilename();
						params[parmLenght++] = downLoadFileList.get(i).getUserid();
				}
				b = sqlHelper.executeUpdate(sql, params);
			}	
		} catch (Exception e) {
			// TODO: handle exception
		}
		return b;
	}
	/**
	 * 查询用户下载的文件记录（下载时间+文件名）
	 * @param userid
	 * @return
	 */
	public List<DownloadFileBean> getUserDownloadFileRecord(String userid){
		if(downLoadFileList!=null){
			downLoadFileList.clear();
    	}
		String sql = "select userid,aFilename,downloadTime from download_file where userid=?  order BY downloadTime DESC";
		String[] params = new String[1];
		params[0] = userid;
		try {
			ArrayList<Object> templist = sqlHelper.executeQuery(sql, params);
			if (!templist.isEmpty()) {
				for (int i = 0; i < templist.size(); i++) {
					Object[] objects = (Object[]) templist.get(i);
					DownloadFileBean downloadFileBean = new DownloadFileBean();
					downloadFileBean.setUserid(objects[0].toString());
					downloadFileBean.setaFilename(objects[1].toString());
					downloadFileBean.setDownloadTime(objects[2].toString());
					downLoadFileList.add(downloadFileBean);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return downLoadFileList;
	}
}
